package com.schoolai.ai.feign;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolai.ai.service.SchoolAiPromptService;
import com.schoolai.entity.SchoolAiDietaryRequirements;
import com.schoolai.entity.SchoolAiPrompt;
import com.schoolai.entity.SchoolStudyEnglishWorkTask;
import com.schoolai.feign.IFeignSchoolDietaryRequirementsontroller;
import com.schoolai.feign.ai.IFeignAIController;
import com.schoolai.feign.study.IFeignSchoolStudyEnglishWorkTaskController;
import com.schoolai.util.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright(C),2019-2025，XX公司
 * FileName:FeignDietaryRecommendController
 * Author:bobby
 * 创建时间：2025/9/25 16:02
 * Description:学生午餐AI推荐
 * History:
 * <auth>        <time>       <version>       <desc>
 * 作者          修改时间       版本号         描述
 */
@Slf4j
@RestController
public class FeignAiController implements IFeignAIController {
    /// 嵌入式模型
    @Autowired
    EmbeddingModel embeddingModel;
    /// AI提示词
    @Autowired
    SchoolAiPromptService schoolAiPromptService;


    @Autowired
    IFeignSchoolDietaryRequirementsontroller iFeignSchoolAiDietaryRequirementsController;
    @Autowired
    @Qualifier("CustomVectorStore")
    VectorStore vectorStore;
    @Autowired
    IFeignSchoolStudyEnglishWorkTaskController iFeignSchoolStudyEnglishWorkTaskController;

    @Autowired
    IFeignSchoolDietaryRequirementsontroller iFeignSchoolDietaryRequirementsontroller;
    private final ChatClient chatClient;
    public FeignAiController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @Override
    public String lunchRecommendation() {
        Result result = schoolAiPromptService.queryList("乌当实验二小午餐要求");
        List<SchoolAiPrompt> SchoolAiPromptList = new ArrayList<>();
        SchoolAiPromptList = (List<SchoolAiPrompt>) result.getData();
        if(SchoolAiPromptList==null||SchoolAiPromptList.size()==0){
            return "";
        }
        /// 3、使用deepseek模型回答
        String aiRecommendation = chatClient.prompt()
                .system(SchoolAiPromptList.get(0).getSystemUser())
                .user(SchoolAiPromptList.get(0).getPromptContent())
                //配置一个日志拦截器，方便查看Rag查到内容
                .advisors(
                        //向量数据库高级查询，一般都只调topK和similarityThreshold的阈值来让文档更精确
                        QuestionAnswerAdvisor.builder(vectorStore)
                                .searchRequest(SearchRequest.builder()
                                        .topK(5)
                                        .similarityThreshold(0.5)
                                        .build()
                                ).build()
                )
                .call()
                .content();
        return aiRecommendation;
    }

    @Override
    public Flux<String> lunchRecommendationByStream() {
        return chatClient.prompt()
                .system("你是一个智能助手")
                .user("根据乌当区实验小学餐饮要求推荐菜谱")
                //配置一个日志拦截器，方便查看Rag查到内容
                .advisors(
                        // 向量数据库高级查询，一般都只调topK和similarityThreshold的阈值来让文档更精确
                        QuestionAnswerAdvisor.builder(vectorStore)
                                .searchRequest(SearchRequest.builder()
                                        .topK(5)
                                        .similarityThreshold(0.5)
                                        .build()
                                ).build()
                ).stream().content();
    }

    @Override
    public Result ttsAI() {

        return Result.ok("转化语音成功");
    }

    @Override
    public Result createArticleByNewWorks(String id) {
        Result result = schoolAiPromptService.queryList("新单词组成文章");
        List<SchoolAiPrompt> SchoolAiPromptList = new ArrayList<>();
        SchoolAiPromptList = (List<SchoolAiPrompt>) result.getData();
        if(SchoolAiPromptList==null||SchoolAiPromptList.size()==0){
            return Result.ok("无提示词");
        }
        List<String> newWorks = new ArrayList<>();
        SchoolStudyEnglishWorkTask schoolStudyEnglishWorkTask = (SchoolStudyEnglishWorkTask) iFeignSchoolStudyEnglishWorkTaskController.getTaskAndListById(id);
        /// 添加提示词
        schoolStudyEnglishWorkTask.getSchoolStudyEnglishWorkTaskList().forEach(
                e->newWorks.add(e.getWord())
        );
        // newWorks = List.of("Hi","Hello","Nice to meet you","I can share!","I can help you!");
        String newWordList = String.join(",", newWorks);
        String arieleConetnt = chatClient.prompt()
                .system(SchoolAiPromptList.get(0).getSystemUser())
                .user(SchoolAiPromptList.get(0).getPromptContent()+newWordList).call().content();
        return Result.ok(arieleConetnt);
    }
}
